import java.io.DataInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Scanner;

import javax.swing.JOptionPane;



public class main {

	/**
	 * @param args
	 * @throws FileNotFoundException 
	 */
	public static void main(String[] args) throws FileNotFoundException {		
			//gdzie jest plik
		 	File f = new File("C:/Ankieta.txt");
		 	File f2 = new File("C:/Glos.txt");
		 	//wczytywanie z pliku
		 	Scanner in;
		 	in = new Scanner(f);

	
		 	String line = "";
		 	String line2 = "";
		 	String line3 = "";
		 	
		 	while(in.hasNext()){	
		 		line += in.next();	
		 	}
		 	int i = 0;
		 	System.out.println("Encrypting String: " + line); 
		 	while(line.charAt(i)!=':'){
	 		 	i++;
		 	}	
		 	i++;
		 	while(line.charAt(i)!=':'){
		 		 	line2 += line.charAt(i);
		 		 	i++;
		 	}
		 	i++;
			while(line.charAt(i)!=':'){
	 		 	line3 += line.charAt(i);
	 		 	i++;
			}
			line ="";
			in = new Scanner(f2);
			while(in.hasNext()){	
		 		line += in.next();	
		 	}
			BigInteger a = new BigInteger(line2);
			BigInteger b = new BigInteger(line3);
			
		 	//kodowanko
		 	RSA rsa = new RSA(a,b);  
		 	// w glownym programie usunac 
		    System.out.println("Encrypting String: " + line);  
		    // w glownym programie usunac system out print 
		    System.out.println("String in Bytes: " + bytesToString(line.getBytes()));  
		    //encrypt
		 	byte[] encrypted = rsa.encrypt(line.getBytes()); 
		 	
		 	kodowanie(line, rsa,encrypted);
		 	
			line = "";
		 	while(in.hasNext())
			 	line += in.nextLine();	
		 	
		 //	dekodowanie(line,rsa,encrypted);

	}
	
	 private static String bytesToString(byte[] encrypted) {  
	        String test = "";  
	        for (byte b : encrypted) {  
	            test += Byte.toString(b);  
	        }  
	        return test;  
	    }  
	 
	 private static void kodowanie(String line, RSA rsa,byte[] encrypted){ 
		    // w glownym programie usunac system out print 
		    System.out.println("Encrypted String in Bytes: " + bytesToString(encrypted));  
		         
		    //zapisywanie do pliku
	 		PrintWriter zapis;
	 	
	 		try {
	 			zapis = new PrintWriter("C:/Glos.txt");
	 		} catch (FileNotFoundException e) {
	 			System.out.println("Blad podczad kodowania lub glos nie istnieje");
	 			return;
	 		}
	 	
	 		zapis.println(bytesToString(encrypted));
	    
	 		zapis.close();
	 }
	 
	/*private static void dekodowanie(String line, RSA rsa,byte[] encrypted){
		 	bytesToString(line.getBytes());
	 		// decrypt = rozkodowanko  
		    byte[] decrypted = rsa.decrypt(encrypted);      
		    // w glownym programie usunac 
		    System.out.println("Decrypted String in Bytes: " +  bytesToString(decrypted));  
		    // w glownym programie usunac system out print   
		    System.out.println("Decrypted String: " + new String(decrypted));  
	 }*/
	 
}

